package com.cheer.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cheer.dao.OaIntegralDetailDao;
import com.cheer.dao.OaLearnEntryDao;
import com.cheer.dao.OaUserDao;
import com.cheer.dto.OaLearnNumberDTO;
import com.cheer.service.OaAsyncService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @author cheer
 */
@Service
public class OaAsyncServiceImpl implements OaAsyncService {

    @Autowired
    private OaUserDao oaUserDao;

    @Autowired
    private OaIntegralDetailDao oaIntegralDetailDao;

    @Autowired
    private OaLearnEntryDao oaLearnEntryDao;

    @Override
    @Async
    public Future<Integer> countUser() {
        return new AsyncResult<>(oaUserDao.selectCount(Wrappers.emptyWrapper()));
    }

    @Override
    @Async
    public Future<Integer> countByType(Integer type) {
        return new AsyncResult<>(oaIntegralDetailDao.selectCountByType(type));
    }

    @Override
    @Async
    public Future<List<OaLearnNumberDTO>> listByLearnIds(List<Integer> learnIds) {
        if (CollectionUtils.isEmpty(learnIds)) {
            return new AsyncResult<>(new ArrayList<>());
        }

        return new AsyncResult<>(oaLearnEntryDao.selectLearnNumberByLearn(learnIds));
    }

    @Override
    @Async
    public Future<Integer> countByLearnId(Integer learnId) {
        return new AsyncResult<>(oaLearnEntryDao.selectCountByLearnId(learnId));
    }

    @Override
    @Async
    public Future<Integer> countByTypeAndItem(Integer type, Integer itemId) {
        return new AsyncResult<>(oaIntegralDetailDao.selectCountByTypeAndItem(type, itemId));
    }

    @Override
    @Async
    public Future<Integer> countByTypeAndTime(Integer type, Integer startTime) {
        return new AsyncResult<>(oaIntegralDetailDao.selectCountByTypeAndTime(type, startTime));
    }
}
